﻿package mortal.game.view.rebate
{
    import extend.language.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.FirstRechargeDay500.*;
    import mortal.game.view.FirstRechargeDay500.rechargeDaily.*;
    import mortal.game.view.rebate.data.*;
    import mortal.game.view.rebate.dayRecharge.*;
    import mortal.game.view.rebate.dayTarget.*;
    import mortal.game.view.rebate.item.*;
    import mortal.game.view.rebate.openServerSevenDay.*;
    import mortal.game.view.rebate.openServerSevenDay.data.*;
    import mortal.game.view.rebate.openServerSevenDay.item.*;
    import mortal.game.view.rebate.shop.panel.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class RebateController extends Controller
    {
        private var _rebateModule:RebateModule;
        public var openServerSevenDayController:OpenServerSevenDayController;
        public var targetC:RebateDayTargetController;
        public var recharge:RebateRechargeController;
        private var _proxy:RebateProxy;
        private var activeRecharge:RechargeDayController;
        private var _stageShow:Boolean = false;
        public static var isShopOpen:Boolean = false;

        public function RebateController()
        {
            this.openServerSevenDayController = new OpenServerSevenDayController();
            this.targetC = new RebateDayTargetController();
            this.recharge = new RebateRechargeController();
            this.activeRecharge = new RechargeDayController();
            this._proxy = GameProxy.rebate;
            return;
        }// end function

        override protected function initServer() : void
        {
            EventUtil.addNetEvent(ServerCommand.RebateActionOpenOrClose, this.onCheckAction, true);
            Dispatcher.addEventListener(EventName.NavbarButtonUIRowInited, this.allowShowWin);
            EventUtil.addSelfEvent(EventName.RebateShowRewardIconEffect, this.onIconEffcteHd, true);
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.levelUpgrade);
            EventUtil.addNetEvent(ServerCommand.NewDayUpdate, this.onCheckAction, true);
            EventUtil.addSelfEvent(EventName.RechargeDailyGetRw, this.onRechargeDailyGetHd, true);
            EventUtil.addNetEvent(ServerCommand.RechargeDailyGetFlag, this.freshRechargeDailyIcon, true);
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._rebateModule == null)
            {
                this._rebateModule = new RebateModule();
                this._rebateModule.addEventListener(WindowEvent.SHOW, this.onWindowShow);
                this._rebateModule.addEventListener(WindowEvent.CLOSE, this.onWindowClose);
            }
            return this._rebateModule;
        }// end function

        private function onWindowShow(event:Event) : void
        {
            this.openServerSevenDayController.onShow(event);
            this.addEvents(true);
            this._rebateModule.selectFirstTatOnload();
            return;
        }// end function

        private function onWindowClose(event:Event) : void
        {
            this.openServerSevenDayController.onClose(event);
            this.addEvents(false);
            return;
        }// end function

        private function addEvents(param1:Boolean) : void
        {
            if (isShopOpen)
            {
                EventUtil.addSelfEvent(EventName.RebateShopGetPanicBuyConfig, this.getShopCfgReq, param1);
                EventUtil.addNetEvent(ServerCommand.RebateShopGetSOnsellItemsMsg, this.onUpdateItemsNum, param1);
                EventUtil.addNetEvent(ServerCommand.RebateShopGetSOnsellPlayerMsg, this.onUpdatePlayerRecord, param1);
                EventUtil.addSelfEvent(EventName.RebateShopBuyItem, this.onBuyItem, param1);
                EventUtil.addSelfEvent(EventName.RebateShopClose, this.onCloseShop, param1);
            }
            EventUtil.addNetEvent(ServerCommand.RebateDayChargeInfoBack, this.onDayRechargeInfo, param1);
            EventUtil.addSelfEvent(EventName.RebateDayRechargeGetReward, this.onDayRechargeGetHd, param1);
            EventUtil.addNetEvent(ServerCommand.RebateDayChargeGetRewardBack, this.onGetDayRechargeBackHd, param1);
            EventUtil.addSelfEvent(EventName.RebateDayRechargeGoToRecharge, this.onGotoRechargeHd, param1);
            EventUtil.addSelfEvent(EventName.RebateDayTargetInfoReq, this.onGetHitTargetInfoReq, param1);
            EventUtil.addNetEvent(ServerCommand.RebateDayTargetInfoBack, this.onDayTargetInfoBackHd, param1);
            EventUtil.addSelfEvent(EventName.RebateDayTargetGetReward, this.onGetDayTargetHd, param1);
            EventUtil.addNetEvent(ServerCommand.RebateDayTargetGetRewardBack, this.onDayTargetGetRewardBack, param1);
            EventUtil.addSelfEvent(EventName.RebateShowRewardTabEffect, this.onTabEffectHd, param1);
            return;
        }// end function

        private function onCheckAction(param1:Object) : void
        {
            if (OpenServerSevenDayUtil.isNotOpenSpeActive())
            {
                return;
            }
            if (this._stageShow == false)
            {
                return;
            }
            if (RebateUtil.isActionOpen(1) && Cache.instance.rebate.oneSevenType != 0)
            {
                OpenServerSevenIcon.instance.show();
                this.onIconEffcteHd(null);
            }
            else
            {
                if (OpenServerSevenIcon.hasInstance())
                {
                    OpenServerSevenIcon.instance.hide();
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.RebateOpenServerSevenDayModuleClose));
            }
            if (RebateUtil.isActionOpen(2) && RebateUtil.isDayTargetOpen())
            {
                RebateDayTargetIcon.instance.show();
                this.onIconEffcteHd(null);
            }
            if (RebateUtil.isActionOpen(3) && RebateUtil.isDayRechargeOpen())
            {
                RebateDayRechargeIcon.instance.show();
                this.onIconEffcteHd(null);
            }
            return;
        }// end function

        private function allowShowWin(param1:Object) : void
        {
            this._stageShow = true;
            this.onCheckAction(null);
            return;
        }// end function

        private function onOpenModule(param1:Object) : void
        {
            GameManager.instance.popupView(GameController.rebate.view);
            return;
        }// end function

        private function onUpdateItemsNum(param1:Object) : void
        {
            var _loc_2:Array = null;
            if (this._rebateModule && this._rebateModule.curPanel && this._rebateModule.curPanel is RebateShopPanel)
            {
                _loc_2 = param1 as Array;
                (this._rebateModule.curPanel as RebateShopPanel).updateItemNum(_loc_2);
            }
            return;
        }// end function

        private function onUpdatePlayerRecord(param1:Object) : void
        {
            var _loc_2:Array = null;
            if (this._rebateModule && this._rebateModule.curPanel && this._rebateModule.curPanel is RebateShopPanel)
            {
                _loc_2 = param1 as Array;
                (this._rebateModule.curPanel as RebateShopPanel).updatePlayerRecord(_loc_2);
            }
            return;
        }// end function

        private function getShopCfgReq(param1:Object) : void
        {
            this._proxy.getSaleConfig();
            return;
        }// end function

        private function onCloseShop(param1:Object) : void
        {
            this._proxy.closeSalePanel();
            return;
        }// end function

        private function onBuyItem(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            var _loc_3:* = int(_loc_2.itemCode);
            var _loc_4:* = _loc_2.item;
            var _loc_5:* = Cache.instance.rebate.curShopId;
            if (_loc_3 != 0 && _loc_5 != -1)
            {
                this._proxy.buySellItem(_loc_5, _loc_3, _loc_4);
            }
            return;
        }// end function

        private function onDayRechargeInfo(param1:Object) : void
        {
            var _loc_2:int = 0;
            if (this._rebateModule && this._rebateModule.curPanel && this._rebateModule.curPanel is RebateDayRechargePanel)
            {
                _loc_2 = param1 as int;
                (this._rebateModule.curPanel as RebateDayRechargePanel).setData();
            }
            return;
        }// end function

        private function onDayRechargeGetHd(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            GameProxy.role.getRechargeTargetReward(_loc_2);
            return;
        }// end function

        private function onGetDayRechargeBackHd(param1:Object) : void
        {
            var _loc_2:int = 0;
            Dispatcher.dispatchEvent(new DataEvent(EventName.RebateShowRewardIconEffect));
            Dispatcher.dispatchEvent(new DataEvent(EventName.RebateShowRewardTabEffect));
            if (this._rebateModule && this._rebateModule.curPanel && this._rebateModule.curPanel is RebateDayRechargePanel)
            {
                _loc_2 = param1 as int;
                (this._rebateModule.curPanel as RebateDayRechargePanel).getRewardBack(_loc_2);
            }
            if (this._rebateModule && this.hasOtherReward() == false)
            {
                this._rebateModule.hide();
            }
            return;
        }// end function

        private function onGotoRechargeHd(event:DataEvent) : void
        {
            GameUtil.gotoRecharge(ModuleTypeConst.RebateModuel);
            return;
        }// end function

        private function onRechargeDailyGetHd(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            GameProxy.player.getRechargeTargetReward(_loc_2);
            return;
        }// end function

        private function freshRechargeDailyIcon(param1:Object) : void
        {
            if (RechargeDailyUtil.canShow() == false)
            {
                if (FirstRechargeDay500Icon.hasInstance())
                {
                    FirstRechargeDay500Icon.instance.hide();
                }
            }
            if (FirstRechargeDay500Icon.hasInstance())
            {
                if (RechargeDailyUtil.hasRw() == false)
                {
                    FirstRechargeDay500Icon.instance.stopEffect();
                }
            }
            return;
        }// end function

        private function onGetHitTargetInfoReq(event:DataEvent) : void
        {
            var _loc_2:* = RebateUtil.getTargetType();
            GameProxy.rebate.getHitTargetInfo(_loc_2);
            return;
        }// end function

        private function onDayTargetInfoBackHd(param1:Object) : void
        {
            if (this._rebateModule && this._rebateModule.curPanel && this._rebateModule.curPanel is RebateDayTargetPanel)
            {
                (this._rebateModule.curPanel as RebateDayTargetPanel).setData();
            }
            return;
        }// end function

        private function onGetDayTargetHd(event:DataEvent) : void
        {
            var _loc_2:* = RebateUtil.getType();
            var _loc_3:* = RebateUtil.getTargetType();
            var _loc_4:* = event.data.targetLevel as int;
            var _loc_5:* = event.data.id as int;
            GameProxy.rebate.getTargetActiveReward(_loc_5, _loc_2, _loc_3, _loc_4);
            return;
        }// end function

        private function onDayTargetGetRewardBack(param1:Object) : void
        {
            var _loc_2:int = 0;
            if (this._rebateModule && this._rebateModule.curPanel && this._rebateModule.curPanel is RebateDayTargetPanel)
            {
                _loc_2 = param1.targetLevel;
                (this._rebateModule.curPanel as RebateDayTargetPanel).updateByLevel(_loc_2);
                MsgManager.showRollTipsMsg("" + Language.getString(71925));
            }
            if (this._rebateModule && this.hasOtherReward() == false)
            {
                this._rebateModule.hide();
            }
            return;
        }// end function

        private function checkHasReward() : void
        {
            return;
        }// end function

        private function onIconEffcteHd(event:DataEvent) : void
        {
            if (OpenServerSevenIcon.hasInstance())
            {
                if (this.hasSevenDayRe())
                {
                    OpenServerSevenIcon.instance.playEffect();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.RebateOpenServerSevenDayModule));
                }
                else
                {
                    OpenServerSevenIcon.instance.stopEffect();
                }
            }
            if (RebateDayRechargeIcon.hasInstance())
            {
                if (this.hasDayRecharge())
                {
                    RebateDayRechargeIcon.instance.playEffect();
                }
                else
                {
                    RebateDayRechargeIcon.instance.stopEffect();
                }
                if (Cache.instance.rebate.hasGetAllDayRechargeReward())
                {
                    RebateDayRechargeIcon.instance.hide();
                }
            }
            if (RebateDayTargetIcon.hasInstance())
            {
                if (this.hasDayTargetRe())
                {
                    RebateDayTargetIcon.instance.playEffect();
                }
                else
                {
                    RebateDayTargetIcon.instance.stopEffect();
                }
            }
            if (FirstRechargeDay500Icon.hasInstance())
            {
                if (RechargeDailyUtil.hasRw())
                {
                    FirstRechargeDay500Icon.instance.playEffect();
                }
                else
                {
                    FirstRechargeDay500Icon.instance.stopEffect();
                }
            }
            return;
        }// end function

        private function levelUpgrade(event:Event) : void
        {
            this.onCheckAction(null);
            return;
        }// end function

        private function hasOtherReward() : Boolean
        {
            if (this.hasSevenDayRe())
            {
                return true;
            }
            if (this.hasDayTargetRe())
            {
                return true;
            }
            if (this.hasDayRecharge())
            {
                return true;
            }
            return false;
        }// end function

        public function hasSevenDayRe() : Boolean
        {
            if (!RebateUtil.isOpenSevenDayOpen())
            {
                return false;
            }
            if (Cache.instance.rebate.hasOneSevenReward == 1)
            {
                return true;
            }
            return false;
        }// end function

        public function hasDayTargetRe() : Boolean
        {
            if (!RebateUtil.isDayTargetOpen())
            {
                return false;
            }
            if (Cache.instance.rebate.hasDayTargetReward == 1)
            {
                return true;
            }
            return false;
        }// end function

        public function hasGetAllTargetReward() : Boolean
        {
            var _loc_3:Object = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_1:* = RebateUtil.getDayTargetList();
            if (_loc_1 == null)
            {
                return false;
            }
            if (Cache.instance.rebate.dayTargetLevelObj == null)
            {
                return false;
            }
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1.length)
            {
                
                _loc_3 = _loc_1[_loc_2];
                _loc_4 = _loc_3.targetLevel;
                _loc_5 = Cache.instance.rebate.dayTargetLevelObj[_loc_4];
                if (_loc_5 != 2)
                {
                    return false;
                }
                _loc_2++;
            }
            return true;
        }// end function

        public function hasDayRecharge() : Boolean
        {
            if (!RebateUtil.isDayRechargeOpen())
            {
                return false;
            }
            return Cache.instance.rebate.hasDayRechargeReward();
        }// end function

        private function onTabEffectHd(event:DataEvent) : void
        {
            if (this._rebateModule && this._rebateModule.tabPanel)
            {
                this.checkSevenDayTab();
                this.checkDayTargetTab();
                this.checkDayRechargeTab();
                this.openFirstHasRewardTab();
            }
            return;
        }// end function

        private function openFirstHasRewardTab() : void
        {
            if (this._rebateModule == null || this._rebateModule.tabPanel == null)
            {
                return;
            }
            if (this.hasSevenDayRe())
            {
                this._rebateModule.openHasRewardByName(RebateTitleConst.SevenDay);
            }
            else if (this.hasDayTargetRe())
            {
                this._rebateModule.openHasRewardByName(RebateTitleConst.DayTarget);
            }
            else if (this.hasDayRecharge())
            {
                this._rebateModule.openHasRewardByName(RebateTitleConst.DayRecharge);
            }
            return;
        }// end function

        private function checkSevenDayTab() : void
        {
            if (this.hasSevenDayRe())
            {
                this._rebateModule.tabPanel.setHasRewardEffect(RebateTitleConst.SevenDay, true);
            }
            else
            {
                this._rebateModule.tabPanel.setHasRewardEffect(RebateTitleConst.SevenDay, false);
            }
            return;
        }// end function

        private function checkDayTargetTab() : void
        {
            if (this.hasDayTargetRe())
            {
                this._rebateModule.tabPanel.setHasRewardEffect(RebateTitleConst.DayTarget, true);
            }
            else
            {
                this._rebateModule.tabPanel.setHasRewardEffect(RebateTitleConst.DayTarget, false);
            }
            return;
        }// end function

        private function checkDayRechargeTab() : void
        {
            if (this.hasDayRecharge())
            {
                this._rebateModule.tabPanel.setHasRewardEffect(RebateTitleConst.DayRecharge, true);
            }
            else
            {
                this._rebateModule.tabPanel.setHasRewardEffect(RebateTitleConst.DayRecharge, false);
            }
            return;
        }// end function

    }
}
